import 'package:flutter/material.dart';
import 'package:flutter_small_project/pages/home/index.dart';
import 'package:flutter_small_project/pages/mine/index.dart';
import 'package:flutter_small_project/utils/eventBus.dart';
import 'package:flutter_small_project/utils/tokenManager.dart';

class TabsPage extends StatefulWidget {
  const TabsPage({super.key});

  @override
  _TabsPageState createState() => _TabsPageState();
}

class _TabsPageState extends State<TabsPage> {
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    // 初识化token,一打开应用的时候初始化token
    tokenManager.init();
    // 尽早的订阅事件
    eventBus.on<LoyoutEvent>().listen(
      (event){
        // print('layout $event');
        // 跳转到登录页
        // context 是当前 widget 所处的上下文环境，它不是你需要手动传入的变量，而是框架自动绑定的。
        Navigator.pushNamed(context, '/login');
      }
      );
  }


  final List _tabBarlist = [
    {
      'lable':'首页',
      'icon':'assets/tabs/home_default.png',
      'activeIcon':'assets/tabs/home_active.png',
    },
    {
      'lable':'我的',
      'icon':'assets/tabs/my_default.png',
      'activeIcon':'assets/tabs/my_active.png'
    }];
   int _activeIndex = 0;


  changreActiveIndex(int index){
    setState(() {
      _activeIndex = index;
    });
  }
  // getTabbarContent(){
  //   if(_currentIndex == 0){
  //     return const HomePage();
  //   }else if(_currentIndex == 1){
  //     return const MinePage();
  //   }
  // }
    List<BottomNavigationBarItem> getBottonNavigationBarItem(){
    List<BottomNavigationBarItem> list = [];
    for(var item in _tabBarlist){
      list.add(BottomNavigationBarItem(
        icon: Image.asset(item['icon'],width: 30,height: 30,),
        activeIcon: Image.asset(item['activeIcon'],width: 40,height: 30,),
        label: item['lable']
      ));
    }
    return list;
  }
  @override
  Widget build(BuildContext context) {
    /* 
    [问题]:图片在鸿蒙端会显示在安全区
    [解决]:在SafeArea中包裹Container即可
     */
    return Scaffold(
      body: SafeArea(
        // child: getTabbarContent(),
        child: IndexedStack(
          index: _activeIndex,
          children: [const HomePage(),MinePage(currentIndex:_activeIndex,
                      changreActiveIndex:changreActiveIndex

          )],
          ),
        ),
      
      bottomNavigationBar: BottomNavigationBar(
        type:BottomNavigationBarType.fixed,
        currentIndex: _activeIndex,
        selectedItemColor: const Color.fromRGBO(99, 144, 174, 1),
        unselectedItemColor: Colors.black,
        items:getBottonNavigationBarItem(),
        onTap: (int index){
          setState(() {
            _activeIndex = index;
          });
        }  ,
      )
    );
  }
}
